﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Configuration;

namespace UmSoft.SharePoint.Integration.Bdc.Core
{
    public class Sattings
    {
        private XmlDocument doc;
        public Sattings()
        {
            doc = new XmlDocument();
            doc.Load(ConfigurationManager.AppSettings["pathApp"]);
        }

        public string GetAttribute(string reportName, string attributeName)
        {
            string s = "";
            foreach (XmlNode node in doc.LastChild.ChildNodes)
            {
                if (node.Attributes["name"].Value == reportName)
                {
                    s = node.Attributes[attributeName].Value;
                    break;
                }
            }
            return s;
        }

        public string GetNameReport(int reportNum)
        {
            string s = "";
            int i=0;
            foreach (XmlNode node in doc.LastChild.ChildNodes)
            {
                i++;
                if (i == reportNum)
                {
                    s = node.Attributes["name"].Value;
                    break;
                }
            }
            return s;
        }

        public string GetDisplayNameReport(int reportNum)
        {
            string s = "";
            int i = 0;
            foreach (XmlNode node in doc.LastChild.ChildNodes)
            {
                i++;
                if (i == reportNum)
                {
                    s = node.Attributes["displayName"].Value;
                    break;
                }
            }
            return s;
        }

        public bool isFileAccess(string path, string user)
        {
            System.IO.FileInfo fi = new System.IO.FileInfo(path);
            System.Security.AccessControl.FileSecurity fs = fi.GetAccessControl();
            System.Security.AccessControl.AuthorizationRuleCollection col =  fs.GetAccessRules(true, true,typeof(System.Security.Principal.NTAccount));

            for(int i=0;i<col.Count;i++)
            {
                if (col[i].IdentityReference.Value == user)
                    return true;
            }
            return false;
        }

        public int GetCountReports()
        {
            return doc.LastChild.ChildNodes.Count;
        }
    }
}
